package base.ifunction;

import java.util.function.BiFunction;
import java.util.function.Function;

public class TestBiFunc {
    public static void main(String[] args) {
        testObjApply();
    }

    private static void testApply() {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;

        System.out.println(add.apply(5, 4));
    }

    private interface IMyClass {
        public Integer join(Integer a);
    }

    private static final class MyClass implements IMyClass {
        @Override
        public Integer join(Integer a) {
            return a + 10;
        }
    }

    private static void testObjApply() {
        MyClass myClass = new MyClass();
        BiFunction<MyClass, Integer, Integer> add = (a, b) -> a.join(b);
        //虽然join只接受一个参数, 而apply接受两个参数
        //如果使用方法引用时, java编译器会自动展开
        // IMyClass::join 将被展开为 (IMyClass a, Integer b) -> a.join(b), 使得代码更简洁
        //add = IMyClass::join
        System.out.println(add.apply(myClass, 5));
    }

    private static void testAndThen() {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        Function<Integer, Integer> times2 = a -> a * 2;

        System.out.println(add.andThen(times2).apply(4, 5));
    }
}
